﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ApexInterest
{
   public partial class MainMenu
    {

       SAPbouiCOM.EditText oEdit;
       SAPbouiCOM.ComboBox oCombo;
       public void Bind(SAPbouiCOM.Form oForm, SAPbouiCOM.Application Sbo_application, SAPbobsCOM.Company oCompany)
       {
           try
           {
               SAPbouiCOM.Item oItem;
               CFL_BP_C1(oForm, Sbo_application);
               oItem = (SAPbouiCOM.Item) (oForm.Items.Item("84"));
               oEdit = ((SAPbouiCOM.EditText)(oItem.Specific));
               oEdit.ChooseFromListUID = "CFLL1";
               oEdit.ChooseFromListAlias = "CardCode";
               
               oItem = (SAPbouiCOM.Item)(oForm.Items.Item("76"));
               oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
               COMBO_Currency(oForm, oCombo, oCompany);

               oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000002"));
               oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
               COMBO_EndDebtor(oForm, oCombo, oCompany);

               oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000004"));
               oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
               COMBO_ProjectCode(oForm, oCombo, oCompany);


               oItem = (SAPbouiCOM.Item)(oForm.Items.Item("1000003"));
               oCombo = ((SAPbouiCOM.ComboBox)(oItem.Specific));
               COMBO_PartnerCode(oForm, oCombo, oCompany);


               oForm.DataBrowser.BrowseBy="78";
           }
           catch (Exception ex)
           {
           }

       }
       public void CFL_BP_C1(SAPbouiCOM.Form oForm, SAPbouiCOM.Application SBO_Application)
       {
           try
           {

               SAPbouiCOM.ChooseFromListCollection oCFLs = null;
               SAPbouiCOM.Conditions oCons = null;
               SAPbouiCOM.Condition oCon = null;

               oCFLs = oForm.ChooseFromLists;

               SAPbouiCOM.ChooseFromList oCFL = null;
               SAPbouiCOM.ChooseFromListCreationParams oCFLCreationParams = null;
               oCFLCreationParams = ((SAPbouiCOM.ChooseFromListCreationParams)(SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_ChooseFromListCreationParams)));

               //  Adding 2 CFL, one for the button and one for the edit text.
               oCFLCreationParams.MultiSelection = false;
               oCFLCreationParams.ObjectType = "2";
               oCFLCreationParams.UniqueID = "CFLL1";

               oCFL = oCFLs.Add(oCFLCreationParams);

               //  Adding Conditions to CFL1

               oCons = oCFL.GetConditions();

               oCon = oCons.Add();
               oCon.Alias = "CardType";
               oCon.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
               oCon.CondVal = "C";
               oCFL.SetConditions(oCons);

               oCFLCreationParams.UniqueID = "CFLL2";
               oCFL = oCFLs.Add(oCFLCreationParams);

           }
           catch (Exception ex)
           {
               SBO_Application.MessageBox(ex.Message);
               //Interaction.MsgBox(Information.Err().Description, (Microsoft.VisualBasic.MsgBoxStyle)(0), null);
           }
       }
    
        public void LoadFromXML(string FileName, SAPbouiCOM.Application Sbo_application)
        {
            try
            {
                System.Xml.XmlDocument oXmlDoc = null;

                oXmlDoc = new System.Xml.XmlDocument();

                // load the content of the XML File
                string sPath = null;
                //Application.SBO_Application.

               sPath = System.IO.Directory.GetParent(System.Windows.Forms.Application.StartupPath).ToString();
                sPath = System.IO.Directory.GetParent(sPath).ToString();
               // string St= "\\Apex\\" ;
                FileName = "\\" + FileName;
                oXmlDoc.Load(sPath + "\\Apex" + FileName);

                // load the form to the SBO application in one batch
                string sXML = oXmlDoc.InnerXml.ToString();
                Sbo_application.LoadBatchActions(ref sXML);
            }
            catch (Exception ex)
            {
                Sbo_application.MessageBox(ex.Message);
            }

        }
        public void COMBO_EndDebtor(SAPbouiCOM.Form oForm, SAPbouiCOM.ComboBox oCombo, SAPbobsCOM.Company oCompany)
        {
            try
            {
                SAPbobsCOM.Recordset oRecordSet;
                //SAPbobsCOM.Company oCompany;
                oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRecordSet.DoQuery("SELECT T0.Code, T0.Name FROM [dbo].[@SETUP]  T0 WHERE T0.[Code] ='EndDebtor'");
                string St;
                St = oRecordSet.Fields.Item("Name").Value;
                oRecordSet.DoQuery("SELECT T0.[CardCode], isnull(T0.[CardName],T0.[CardCode]) CardName FROM OCRD T0 WHERE T0.[GroupCode] ='" + St + "'");
                int i;
                for (i = oCombo.ValidValues.Count; i < 0; i--)
                {
                    oCombo.ValidValues.Remove(i, SAPbouiCOM.BoSearchKey.psk_Index);
                }

                for (i = 0; i < oRecordSet.RecordCount; i++)
                {
                    oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value);
                    oRecordSet.MoveNext();
                }
                //oRecordSet = Nothing;
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }
        public void COMBO_PartnerCode(SAPbouiCOM.Form oForm, SAPbouiCOM.ComboBox oCombo, SAPbobsCOM.Company oCompany)
        {
            try
            {
                SAPbobsCOM.Recordset oRecordSet;
                //SAPbobsCOM.Company oCompany;
                oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRecordSet.DoQuery("SELECT T0.Code, T0.Name FROM [dbo].[@SETUP]  T0 WHERE T0.[Code] ='FundedPartner'");
                string St;
                St = oRecordSet.Fields.Item("Name").Value;
                oRecordSet.DoQuery("SELECT T0.[CardCode],isnull(T0.[CardName],T0.[CardCode]) CardName FROM OCRD T0 WHERE T0.[GroupCode] ='" + St + "'");
                int i;
                for (i = oCombo.ValidValues.Count; i < 0; i--)
                {
                    oCombo.ValidValues.Remove(i, SAPbouiCOM.BoSearchKey.psk_Index);
                }

                for (i = 0; i < oRecordSet.RecordCount; i++)
                {
                    oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value);
                    oRecordSet.MoveNext();
                }
                //oRecordSet = Nothing;
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }
        public void COMBO_Currency(SAPbouiCOM.Form oForm, SAPbouiCOM.ComboBox oCombo, SAPbobsCOM.Company oCompany)
        {
            try
            {
                SAPbobsCOM.Recordset oRecordSet;
                //SAPbobsCOM.Company oCompany;
                oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRecordSet.DoQuery("SELECT T0.[CurrCode], T0.[CurrName] FROM OCRN T0");
                int i;
                for (i = oCombo.ValidValues.Count; i < 0; i--)
                {
                    oCombo.ValidValues.Remove(i, SAPbouiCOM.BoSearchKey.psk_Index);
                }

                for (i = 0; i < oRecordSet.RecordCount; i++)
                {
                    oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value);
                    oRecordSet.MoveNext();
                    
                }
                oCombo.Select("SGD", SAPbouiCOM.BoSearchKey.psk_ByValue);
                //oRecordSet = Nothing;
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }
        public void COMBO_ProjectCode(SAPbouiCOM.Form oForm, SAPbouiCOM.ComboBox oCombo, SAPbobsCOM.Company oCompany)
        {
            try
            {
                SAPbobsCOM.Recordset oRecordSet;
                //SAPbobsCOM.Company oCompany;
                oRecordSet = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                oRecordSet.DoQuery("SELECT T0.[PrjCode], isnull(T0.[PrjName],T0.[PrjCode])  [PrjName] FROM OPRJ T0 WHERE T0.[Active] ='Y'");
                int i;
                for (i = oCombo.ValidValues.Count; i < 0; i--)
                {
                    oCombo.ValidValues.Remove(i, SAPbouiCOM.BoSearchKey.psk_Index);
                }

                for (i = 0; i < oRecordSet.RecordCount; i++)
                {
                    oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value);
                    oRecordSet.MoveNext();

                }
               // oCombo.Select("SGD", SAPbouiCOM.BoSearchKey.psk_ByValue);
                //oRecordSet = Nothing;
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }
    }


}
